require.config({
    paths: {
        vue: "../encaptool/encvue",
        iview: "../utiltool/iview/iview",
        jquery: "../utiltool/jquery/jquery.min",
        router: "../encaptool/encarouter",
        axios: "../encaptool/encaxios"
    },
    shim: {
        iview: ["vue"]
    }
});
require(["vue", "iview", "jquery", "router", "axios"], function (
    Vue,
    iview,
    $,
    Router,
    axios
) {
    Vue.use(iview);
    new Vue({
        el: "#uncomplete",
        data() {
            return {
                self: this,
                value4:null,
                loading: false,
                 // 初始化信息总条数
                dataCount: 0,
                 // 每页显示多少条
                pageSize: 50,
                 // 每页信息
                historyData: [],
                columns: [
                    {
                        title: "序号",
                        type:'index',
                        align: "center",
                        width:65,
                    },
                    {
                        title: "送货单号",
                        key: "freightId",
                        align: "center",
                        width:150
                    },
                    {
                        title: '订单号',
                        key: "orderid",
                        align: "center",
                        width:150
                    },
                    {
                        title: "配送车辆",
                        key: "truckid",
                        align: "center",
                        width:150
                    },
                    {
                        title: "订单产生日期",
                        key: "createdate",
                        align: "center",
                        width:170
                    },
                    {
                        title: "订单取消日期",
                        key: "canceldate",
                        align: "center",
                        width:170
                    },
                    {
                        title: "目的地址",
                        key: "bourn",
                        align: "center",
                        width:340
                    },
                    {
                        title: "公里(约)",
                        key: "kilometre",
                        align: "center",
                        "sortable": true,
                        width:100
                    },
                    {
                        title: "是否加急",
                        key: "isEmergency",
                        align: "center",
                        width:140,
                        filters: [{
                                label: '加急单',
                                value: 0
                            },
                            {
                                label: '正常单',
                                value: 1
                            }
                        ],
                        filterMultiple: false,
                        filterMethod(value, row) {
                            if (value === 0) {
                                return row.isEmergency >= 1;
                            } else if (value === 1) {
                                return row.isEmergency <= 0;
                            }
                        },
                        render: (h, params) => {
                            const row = params.row;
                            const color = row.isEmergency === 0 ? 'blue' : row.isEmergency === 1 ? 'red' : 'green';
                            const text = row.isEmergency === 0 ? '正常单' : row.isEmergency === 1 ? '加急单' : '无状态';
                            return h('Tag', {
                                props: {
                                    type: 'dot',
                                    color: color
                                }
                            }, text);
                        }
                    },
                    {
                        title: "备注",
                        key: "mark",
                        align: "center",
                        width: 140
                    },
                    {
                        title: "操作",
                        key: "operation",
                        align: "center",
                        width: 80,
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'info'
                                    },
                                    on: {
                                        click: () => {
                                            this.remove(this.data[params.index].freightId);
                                        }
                                    }
                                }, '恢复')
                            ]);
                        }
                    }
                    // {
                    //     title: "操作",
                    //     key: "operation",
                    //     align: "center",
                    //     render: (h, params) => {
                    //         return h('dropdown', {
                    //             props: {
                    //                 trigger: 'click',
                    //             },
                    //         }, [
                    //             h('Button', {
                    //                 props: {
                    //                     type: 'primary'
                    //                 },
                    //             }, '操作菜单'),
                    //             h('dropdown-menu', {
                    //                 slot: 'list'
                    //             }, [
                    //                 h('dropdown-item',{
                    //                 },[
                    //                     h('a',{
                    //                         on: {
                    //                             click: () => {
                    //                                 //this.change(params.index)
                    //                                 this.warning();
                    //                             }
                    //                         }
                    //                     }, '恢复订单'),
                    //                 ]),
                    //                 h('dropdown-item',{
                    //                 },[
                    //                     h('a',{
                    //                         on: {
                    //                             click: () => {
                    //                                 //this.change(params.index)
                    //                                 //this.handleSelectAll(true);
                    //                                 //this.warning();
                    //                             }
                    //                         }
                    //                     }, '合并订单'),
                    //                 ]),
                    //                 h('dropdown-item',{
                    //                 },[
                    //                     h('a',{
                    //                     on: {
                    //                         click: () => {
                    //                             //this.change(params.index)
                    //                             this.warning();
                    //                         }
                    //                     }
                    //                 }, '拆分订单'),
                    //                 ]),
                    //             ])
                    //         ]);
                    //     }
                    // }
                ],
                data: [],
                showIndex: true,
                setting: {
                    method: "GET",
                    url: "/YF/public/api/getuncompletedata"
                },
                setting1: {
                    method: "GET",
                    url: "/YF/public/api/getACancelOrder/"
                },
                setting2: {
                    method: "GET",
                    url: "/YF/public/api/changeToWaitOrder/"
                }
            };
        },
        mounted() {
            this.getuncompletedata();
        },
        methods: {
            //导出数据excle.csv的
            exportData(type) {
                var myDate = new Date();//获取系统当前时间
                var year = myDate.getFullYear();
                var month = myDate.getMonth()+1;
                var day = myDate.getDate();
                var hour = myDate.getHours();
                var minute = myDate.getMinutes();
                var second = myDate.getSeconds();
                if (type === 1) {
                    this.$refs.selection.exportCsv({
                        filename: year+'年'+month+'月'+day+'日 '+hour+':'+minute+':'+second,
                        original: false,
                        columns: this.columns.filter((col, index) => index < 8),
                        data: this.data.filter((data, index) => index < this.data.length)
                    });
                }
            },
            //获取待完成订单
            getuncompletedata() {
                this.loading = true;
                axios.call(this, function (res) {
                    if (res.code === 0) {
                        this.$refs['pages'].currentPage = 1;
                        this.data = res.data;
                        this.dataCount = res.data.length;
                        if(this.dataCount < this.pageSize) {
                            this.historyData = this.data;
                        }else {
                            this.historyData = this.data.slice(0, this.pageSize);
                        }
                        this.loading = false;
                    } else {
                        this.$Message.info({content:'目前没有异常未完成订单',closable: true});
                        this.loading = false;
                    }
                }.bind(this), {
                    setting: this.setting
                });
            },
            //获取搜索的订单
            getfind() {
                this.loading = true;
                this.setting1.url += this.trim;
                if (this.trim == null) {
                    this.$Message.info({content:'请输入相应查询信息',closable: true});
                    this.loading = false;
                } else {
                    axios.call(this, function (res) {
                        if (res.code === 0) {
                            this.historyData = res.data;
                            this.loading = false;
                            this.setting1.url = '/YF/public/api/getACancelOrder/';
                        } else {
                            this.$Message.info({content:'未查到数据',closable: true});
                            this.setting1.url = '/YF/public/api/getACancelOrder/';
                            this.loading = false;
                        }
                    }.bind(this), {
                        setting: this.setting1
                    });
                }
            },
            //转换成待接订单
            change(index) {
                this.setting2.url += this.data[index].freightId;
                //this.data.splice(index, 1);
                axios.call(this, function (res) {
                    if (res.code === 0) {
                        this.loading = false;
                        this.setting2.url = '/YF/public/api/changeToWaitOrder/';
                        this.$options.methods.getuncompletedata.bind(this)();
                    } else {
                        this.$Message.info({content:'未查到数据',closable: true});
                        this.loading = false;
                    }
                }.bind(this), {
                    setting: this.setting2
                });
            },
            handleSelectAll (status) {
                this.$refs.selection.selectAll(status);
            },
            changePage (index) {
                var _start = ( index - 1 ) * this.pageSize;
                var _end = index * this.pageSize;
                this.historyData = this.data.slice(_start,_end);
            },
            warning (nodesc) {
                this.$Notice.warning({
                    title: '订单操作',
                    desc: nodesc ? '' : '功能待完善，敬请期待！ ',
                    duration: 1.5,
                    closable: true
                });
            },
            remove (freightId) {
                let setting = {
                    method: "get",
                    url: "/YF/public/api/removeFreight/" + freightId
                }
                axios.call(this, function (res) {
                    if (res.code ===0 ) {
                        this.$Message.success({content:'恢复成功',closable: true});
                    }else {
                        this.$Message.info({content:'恢复失败',closable: true});
                    }
                    this.data.splice(0, this.data.length);
                    this.getuncompletedata();
                }.bind(this), {
                    setting: setting
                })
            }
        },
        computed: {
            trim: function () {
                if(this.value4==null)
                {
                    this.value4=null;
                }else
                {
                    return  this.value4.replace(/(^\s*)|(\s*$)/g, "");
                }
            },
        }
    });
});